#!/usr/bin/env bash
set -eo pipefail

BUILDPACK_INSTALL_PATH="/tmp/buildpacks"

download_buildpack() {
    buildpack_url="$1"
    buildpack_name=$(basename $buildpack_url)
    buildpack_commit="$2"

    echo "Fetching $buildpack_name..."

    set +e
    git clone --branch $buildpack_commit --depth 1 $buildpack_url $BUILDPACK_INSTALL_PATH/$buildpack_name &>/dev/null
    SHALLOW_CLONED=$?
    set -e
    if [ $SHALLOW_CLONED -ne 0 ]; then
        # if the shallow clone failed partway through, clean up and try a full clone
        rm -rf $BUILDPACK_INSTALL_PATH/$buildpack_name
        git clone --quiet $buildpack_url $BUILDPACK_INSTALL_PATH/$buildpack_name
        pushd $BUILDPACK_INSTALL_PATH/$buildpack_name &>/dev/null
            git checkout --quiet $buildpack_commit
        popd &>/dev/null
    fi

    echo "Done."
}

mkdir -p $BUILDPACK_INSTALL_PATH

download_buildpack https://github.com/heroku/heroku-buildpack-multi.git          26fa21a
download_buildpack https://github.com/heroku/heroku-buildpack-ruby.git           v137
#download_buildpack https://github.com/heroku/heroku-buildpack-nodejs.git         v75
download_buildpack https://github.com/CloudSide/heroku-buildpack-nodejs.git      v78-hehe
download_buildpack https://github.com/heroku/heroku-buildpack-java.git           v38
download_buildpack https://github.com/heroku/heroku-buildpack-gradle.git         v12
download_buildpack https://github.com/heroku/heroku-buildpack-grails.git         v17
download_buildpack https://github.com/heroku/heroku-buildpack-play.git           v23
download_buildpack https://github.com/heroku/heroku-buildpack-python.git         v58
#download_buildpack https://github.com/heroku/heroku-buildpack-php.git            v67
download_buildpack https://github.com/CloudSide/heroku-buildpack-php.git         v71-hehe
download_buildpack https://github.com/heroku/heroku-buildpack-clojure.git        v66
download_buildpack https://github.com/heroku/heroku-buildpack-scala.git          v55
download_buildpack https://github.com/heroku/heroku-buildpack-go.git             6eeb09f
